package com.xuecheng.teaching.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.commons.constants.CommonConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.WorkDto;
import com.xuecheng.commons.model.vo.*;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.api.ContentClient;
import com.xuecheng.teaching.domain.Work;
import com.xuecheng.teaching.mappers.WorkMapper;
import com.xuecheng.teaching.service.WorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 作业 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-31
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {
        @Autowired
        private ContentClient contentClient;
        /**
         * 分页条件查询课程计划作业列表
         * @return vo
         */
        /*public ResponseResult findByPage(PageRequest pageRequest, WorkQueryDto dto) {
                // 创建分页对象
                Page<Work> page = new Page<>(pageRequest.getPageNo(),pageRequest.getPageSize());
                // 创建查询条件
                LambdaQueryWrapper<Work> qw = new LambdaQueryWrapper<>();
                if(!StringUtils.isEmpty(dto.getTitle())){
                        qw.like(Work::getTitle,dto.getTitle());
                }
                if(!StringUtils.isEmpty(dto.getUsername())){
                        qw.eq(Work::getUsername,dto.getUsername());
                }
                if(dto.getCoursePubId() != null){
                        qw.eq(Work::getCoursePubId,dto.getCoursePubId());
                }
                if(dto.getTeachplanId() != null){
                        qw.eq(Work::getTeachplanId,dto.getTeachplanId());
                }
                if(dto.getStatus() != null){
                        qw.eq(Work::getStatus,dto.getStatus());
                }
                IPage<Work> workPage = this.page(page, qw);
                List<Work> workList = workPage.getRecords();
                List<WorkVo> vos = BeanHelper.copyWithCollection(workList, WorkVo.class);
                return PageResponseResult.okResult(workPage.getTotal(),vos);
        }*/
        @Override
        public ResponseResult findByPage(PageRequest request) {
                //设置分页参数
                LambdaQueryWrapper<Work> QW = new LambdaQueryWrapper<>();
                QW.eq(Work::getStatus, CommonConstants.USING_FLAG);
                QW.eq(Work::getCompanyId, AuthInfoHolder.getCompanyId());
                Page<Work> page = new Page<>(request.getPageNo(),request.getPageSize());
                //查询条件
                IPage<Work> iPage = this.page(page,QW);
                List<Work> workList = iPage.getRecords();
                for (Work work : workList) {
                        if(work.getId() == null){
                                work.setId(Long.valueOf(UUID.randomUUID().toString(true)));
                        }
                }
                List<WorkVo> vos = BeanHelper.copyWithCollection(workList, WorkVo.class);
                return PageResponseResult.okResult(iPage.getTotal(),vos);
        }

        /**
         * 根据id删除
         * @param workId 作业id
         * @return null
         */
        @Override
        public ResponseResult deleteById(Long workId) {
                if(workId == null){
                        throw new BusinessException(ErrorCode.PARAMSERROR);
                }
                this.removeById(workId);
                return ResponseResult.okResult();
        }

        @Override
        public ResponseResult findById(Long workId) {
                if(workId == null){
                        throw new BusinessException(ErrorCode.PARAMSERROR);
                }
                LambdaQueryWrapper<Work> qw = new LambdaQueryWrapper<>();
                qw.eq(Work::getId,workId);
                Work work = this.getOne(qw);
                WorkVo vo = BeanHelper.copyProperties(work, WorkVo.class);
                return ResponseResult.okResult(vo);
        }

        @Override
        public ResponseResult updateOrSave(WorkDto dto) {
                CoursePubVo coursePubVo = contentClient.findById(dto.getCoursePubId()).getData();
                LambdaQueryWrapper<Work> qw = new LambdaQueryWrapper<>();
                qw.eq(Work::getId,dto.getWorkId());
                qw.eq(Work::getUsername,dto.getUsername());
                Work work = this.getOne(qw);
                boolean flag = false;
                if(work != null){
                        // 如果已经存在作业
                        work.setChangeDate(new Date());
                        work.setQuestion(dto.getQuestion());
                        work.setTitle(dto.getTitle());
                        // 更新
                        flag = this.updateById(work);
                }else {
                        // 没有则 新建作业
                        Work newWork = new Work();
                        newWork.setUsername(dto.getUsername());
                        newWork.setCreateDate(new Date());
                        newWork.setChangeDate(new Date());
                        newWork.setCoursePubId(dto.getCoursePubId());
                        newWork.setStatus(dto.getStatus());
                        newWork.setCourseName(coursePubVo.getName());
                        newWork.setTitle(dto.getTitle());
                        newWork.setTeachplanId(dto.getTeachplanId());
                        newWork.setQuestion(dto.getQuestion());
                        flag = this.save(newWork);
                }
                if(!flag){
                        throw new BusinessException(ErrorCode.ERROR);
                }
                return ResponseResult.okResult();
        }
}
